'use strict';
// 初始化交易记录视图
function initView() {
    $("#cost-no").on("touchend",function  () {
        $("#prompt-return_cost").fadeOut(200);
    });
    $("#fanhui").on("touchstart",function  () {
       location.href = "./merchantHome.html";
    });
}
// 创建交易记录
function createRecord(tradeObjects, $parent) {
    var typeName = {
        "01": {
            "btn_name": "01",
            "operType": ""
        },
        "02": {
            "btn_name": "02",
            "operType": ""
        },
        "退货": {
            "btn_name": "退货",
            "operType": "weui-btn weui-btn_plain-primary"
        },
        "撤销": {
            "btn_name": "撤销",
            "operType": "weui-btn weui-btn_plain-primary"
        },
        "完成": {
            "btn_name": "完成",
            "operType": "weui-btn weui-btn_plain-default weui-btn_plain-disabled"
        },
        "退货中": {
            "btn_name": "05",
            "operType": "weui-btn weui-btn_plain-default weui-btn_plain-disabled"
        },
        "退货(非免息)": {
            "btn_name": "06",
            "operType": "weui-btn weui-btn_plain-primary const"
        },
        "退货(不免息)": {
            "btn_name": "06",
            "operType": "weui-btn weui-btn_plain-primary const"
        }
    }
    $parent.empty();
    for (var i = 0, l = tradeObjects.length; i < l; i++) {
        var tradeObject = tradeObjects[i];
        tradeObject.time = changeDateTime(tradeObject.trans_date, "second");
        //        tradeObject.oper = typeName[tradeObject.trans_type].btn_name;
        /***********************/
        var isconst = 0;
        if(tradeObject.trans_type == "退货(不免息)" || tradeObject.trans_type == "退货(非免息)"){
          isconst = 1;
        } else if(tradeObject.trans_type == "撤销"){
          isconst = 2;
        }
        /**********************/
        tradeObject.operType = typeName[tradeObject.trans_type].operType;
        var order_id_str = String(tradeObject.order_id).replace("s","");
        var template = `
            <div class="weui-cells__title">
            <div class="weui-cell__hd">${tradeObject.time}</div>
            <div class="weui-cell__bd">${tradeObject.trans_status}</div>
            </div>
            <div data-trans_seq="${tradeObject.trans_seq}" class="weui-cell">
                <div class="weui-cell__hd"><img class="icon" src="../images/tltd/fenqixiaofei_icon.png"></div>
                <div class="weui-cell__bd">
                    <p class="trade_name">分期消费</p>
                    <p class="trade_money">交易金额<span>${tradeObject.trans_amt}</span></p>
                </div>
                <div class="weui-cell__ft">
                    <p class="trade_no">订单:${order_id_str}</p>
                    <a data-goods_Amount="${tradeObject.goods_Amount}" data-order_id="${order_id_str}" data-cost="${isconst}" data-serial_no="${tradeObject.serial_no}" href="javascript:;" class="${tradeObject.operType}">${tradeObject.trans_type}</a>
                </div>
            </div>`;

        $parent.append(template);
    }

}

// 显示免息期外退货弹框
function showCostDialog(erroMsg) {
    $("#prompt-return_cost").css({
        "opacity": "1",
        "display": ""
    });
    $("#prompt-return_cost").find(".weui-mask").css({
      "opacity": "1",
      "visibility": "inherit"
    })
    $("#prompt-return_cost").find(".weui-dialog").css({
      "opacity": "1",
      "visibility": "inherit"
    })

    //    $("#errorDialog2 .weui-dialog__bd").text(erroMsg);
}

// 显示loading
function showLoadingDialog() {
    $("#loadingDialog").css({
        "opacity": "1",
        "display": ""
    });
}

// 隐藏loading
function hideLoadingDialog() {
    $("#loadingDialog").fadeOut(200);
}


// 时间20161108160023 转为 2016-11-08 16:00:23
function changeDateTime(dateString, type) {
    dateString = String(dateString);
    var pattern = /(\d{4})(\d{2})(\d{2})/;
    var formatedDate;
    if (type == "time") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 12).replace(pattern, '$1-$2-$3 $4:$5');
    } else if (type == "second") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 16).replace(pattern, '$1-$2-$3 $4:$5:$6');
    } else if (type == "date") {
        formatedDate = dateString.substr(0, 8).replace(pattern, '$1-$2-$3');
    }

    return formatedDate;
}


export {
    initView,
    createRecord,
    showCostDialog,
    showLoadingDialog,
    hideLoadingDialog
}
